<?php

namespace App\Admin\Controllers;

use App\Models\Category;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Tree;

class CategoryController extends AdminController
{
    use HasResourceActions;

    protected $title = '所有分类';

    public function index(Content $content)
    {
        return $content
            ->title($this->title)
            ->body($this->tree());
    }

    /**
     * Make a grid builder.
     *
     * @return Tree
     */
    protected function tree()
    {
        return Category::tree(function (Tree $tree) {
            $tree->branch(function ($branch) {
                return "{$branch['id']} - {$branch['name']} ";
            });
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Category());

        $grid->id('ID')->sortable();
        $grid->project()->name('应用');
        $grid->title('分类名');
        $grid->time_out('超时提醒(分钟)');
        $grid->time_out_sequence('超时提醒次数');
        $states = [
            'on' => ['value'=>1,'text' => '正常'],
            'off' => ['value'=>0,'text' => '无效'],
        ];
        $grid->status('状态')->switch($states);

        $grid->actions(function ($actions) {
            $actions->disableView();
            // append一个操作
            $url = "/admin/category/{$actions->getKey()}/forms/";
            $actions->prepend('|&nbsp;<a href="'.$url.'"><i class="fa fa-wpforms"></i>表单管理</a>&nbsp;|');
        });

        $grid->tools(function (Grid\Tools $tools) {
            $tools->batch(function (Grid\Tools\BatchActions $actions) {
                $actions->disableDelete();
            });
        });
        $grid->filter(function($filter){
            $filter->column(1/2, function ($filter) {
                $filter->where(function ($query) {

                    $query->where('title', 'like', "%{$this->input}%");

                }, '关键词')->placeholder('关键词(包括：分类名)');
            });

            $filter->column(1/2, function ($filter) {
                $filter->equal('project_id', '应用')->select(ProjectModel::all()->pluck('name', 'id')->toArray());
                $filter->gt('created_at')->datetime();
                $filter->between('updated_at')->datetime();
                $filter->equal('status')->radio([
                    1 => '正常',
                    0 => '无效',
                ]);
            });

        });

        return $grid;
    }
    /**
     * Make a form builder.
     *
     * @return Form
     */
//    protected function form()
//    {
//        $form = new Form(new Category());
//        $form->display('id', 'ID');
//        //应用分类联动
//        $form->select('project_id', '应用')->options(ProjectModel::all()->pluck('name', 'id')->toArray())->rules('required');
//        $form->text('title', '分类名')->rules('required');
//        $form->textarea('description', '描述')->rules('required');
//        $form->text('sort', '排序值')->rules('required|regex:/^\d+$/', [
//            'regex' => '排序值必须为数字',
//        ]);
//
//        $need_login = [
//            'on'  => ['value' => 1, 'text' => '是', 'color' => 'success'],
//            'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
//        ];
//        $form->switch('need_login', '需要登录')->states($need_login);
//
//        $need_verify = [
//            'on'  => ['value' => 1, 'text' => '是', 'color' => 'success'],
//            'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
//        ];
//        $form->switch('need_verify', '验证码')->states($need_verify);
//
//        $states = [
//            'on'  => ['value' => 1, 'text' => '正常', 'color' => 'success'],
//            'off' => ['value' => 0, 'text' => '无效', 'color' => 'danger'],
//        ];
//        $form->switch('status', '状态')->states($states);
//
//        $form->text('time_out', '超时提醒(分钟)')->rules('required|regex:/^\d+$/', [
//            'regex' => '分钟数必须为数字',
//        ]);
//
//        $form->text('time_out_sequence', '超时提醒次数')->rules('required|regex:/^\d+$/', [
//            'regex' => '超时提醒次数必须为数字',
//        ]);
//        $form->display('created_at', 'Created At');
//        $form->display('updated_at', 'Updated At');
//        return $form;
//    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Category());
        $form->display('id', 'ID');
        $form->select('pid')->options(Category::selectOptions());
        $form->text('name', '名称')->rules('required');
        $form->text('keywords', '关键词')->rules('required');
        $form->textarea('description', '描述')->rules('required');
        $form->number('sort', '排序值');
        $form->display('created_at', 'Created At');
        $form->display('updated_at', 'Updated At');
        return $form;
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header(trans('admin.category'))
            ->description(trans('admin.create'))
            ->body($this->form());
    }

    /**
     * Edit interface.
     *
     * @param $id
     *
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header(trans('admin.category'))
            ->description(trans('admin.edit'))
            ->body($this->form()->edit($id));
    }
}
